<?php

class Sistema_AgendaavaliacaoController extends App_Controller_Action
{
    private $_table     = 'AGENDA_AVALIACAO';
    private $_pk        = 'AGA_ID';
    private $_order_col = 'AGA_DAT';
    private $_order     = 'DESC';
    private $_sta_col   = 'AGA_STA';
    private $obj        = '';
    private $form       = '';
    private $_file_col  = 'AGA_FILE'; 
    public  $dir = 'public/upload//';
    public  $db = '';
    
    public function init()
    {
        if($this->getRequest()->getActionName() == 'edit') $this->form = new Sistema_Form_Agendaavaliacao();
    }

    public function indexAction() {
        
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('datetools','library');
        Zend_Loader::loadClass('dominio','library');
        Zend_Loader::loadClass('elemform','library');
        
        
        $where = $this->_pk . ' > 0';
        
        
        $dtfiltro = $this->_getParam('dtfiltro', ''); 
        $dsfiltro = $this->_getParam('dsfiltro', '');
        $stafiltro = $this->_getParam('stafiltro', '1');
        
        if($dtfiltro != '') $where .= ' AND AGA_DAT = "' .Datetools::dma_amd($dtfiltro).'"';
        if($dsfiltro != '') $where .= ' AND AGA_NOM LIKE "'. $dsfiltro.'%"';
        if($stafiltro != '') $where .= ' AND '.$this->_sta_col.' = '. $stafiltro; 
        
        $this->view->dtfiltro = $dtfiltro;
        $this->view->dsfiltro = $dsfiltro;
        $this->view->stafiltro = $stafiltro;
        
        
        
        $query = 'SELECT SQL_CACHE * FROM '.$this->_table.' WHERE '.$where.' ORDER BY '.$this->_order_col.' '.$this->_order;
        $result = $this->db->fetchAll($query);
        $this->view->result = $this->paginacao($result);
        
    }
    
    public function editAction() {
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_form_pdr.js');
        
        Zend_Loader::loadClass('imagem','library');
        Zend_Loader::loadClass('datetools','library');
        Zend_Loader::loadClass('dominio','library');
        
        $this->view->form = $this->form;
        if($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            try {
                if($this->form->isValid($formData)) {
                    $id = (int) $formData[$this->_pk];

                    $data = $formData;

                    unset($data['img']);
                    unset($data[$this->_file_col]);
                    unset($data['Salvar']);

                    $data['AGA_DAT'] = Datetools::dma_amd($data['AGA_DAT']);
                    
                    if($id == 0){
                        $this->db->insert($this->_table, $data);
                        $id =  $this->db->lastInsertId();   
                    }
                    else $this->db->update($this->_table, $data,$this->_pk.' = ' . $id);

                    $this->_helper->redirector('index');          
                } else {
                    $this->form->populate($formData);
                }
            } catch(Exception $ex) {
                Zend_Loader::loadClass('errordb','library');
                echo '<script>alert("'.$ex->getMessage().'");</script>';
                $this->form->populate($formData);
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $data = $this->db->fetchRow('SELECT SQL_CACHE * FROM '.$this->_table.' WHERE '.$this->_pk . ' = ' . $id);
                $data['AGA_DAT'] = Datetools::amd_dma($data['AGA_DAT']);
                $this->form->populate($data);
            }
        }
    }

    public function deleteAction() {
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = FALSE;
        $id = $this->_getParam('id', 0);
        if ((int) $id > 0 ) {   
            $ok = (bool) $this->db->delete($this->_table, $this->_pk . ' = ' . $id);
        }      
        echo json_encode(array('ok' => $ok));
    }

}